(function() {
	var app = angular.module('WarningConfigApp', [ 'ui.bootstrap' ]);
	app
			.controller(
					'warningConfigController',
					function($http,$scope, $log, $uibModal) {
						var basePath = $scope.getAppBasePath();
						$scope.insertComponent = {};
						$scope.deleteComponent = {};
						var controller = this;
						controller.parameter = {};
						controller.queryParameter = "";
						this.warningConfigList = [];
						this.indexList = [];
						this.queryCondition = {
							"monitoringIndex" : "",
							"page" : 1,
							"size" : 5,
						};
						this.paginationInfo = {
							"maxSize" : 5,
							"totalPages" : 0,
							"totalElements" : 0
						};

						$scope.clickEvent = function() {
							controller.queryCondition.page = controller.queryCondition.forwordPage;
							loadData();
						}

						// 跳转页数，回车事件
						$scope.enterEvent = function(e) {
							var keycode = window.event ? e.keyCode : e.which;
							if (keycode == 13) {
								$scope.clickEvent();
							}
						}

						this.pageChanged = function() {
							loadData();
						}
						// 完成全选全部选事件
						$("#check_all").click(
								function() {
									$(".check_item").prop("checked",
											$(this).prop("checked"));
								});
						// 选中单个元素，如果单个元素选择满了，那么第一个主元素也被选中
						$(document)
								.on(
										"click",
										".check_item",
										function() {
											if ($(".check_item:checked").length == $(".check_item").length) {
												$("#check_all").prop("checked",
														true);
											}
											if ($(".check_item:checked").length == 0) {
												$("#check_all").prop("checked",
														false);
											}
										});
						
						
						// 新增配置
						this.insertConfig = function() {
							$scope.insertComponent = {};
							var modalInstanceAdd = $uibModal
									.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										// animation :
										// $scope.animationsEnabled,// 打开时的动画开关
										templateUrl : 'js/controller/configuration.management/insertWarningConfig.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'insertWarningConfigController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									// size : "lg",// 模态框的大小尺寸
									});

							modalInstanceAdd.result.then(function(result) {
								loadData();
							});
						}
						
						//删除配置
						this.delConfig = function() {
							var basePath = $scope.getAppBasePath();
							var conditons =  new Array();
							var ids = "";
							$.each($(".check_item:checked"), function() {
								var id = "";   
								id = $(this).parents("tr").find("td:eq(1)").text();
								
								conditons.push(id);
								ids += $(this).parents("tr").find("td:eq(1)").text()+",";
							})
							ids = ids.substring(0,ids.length-1);
							console.log(conditons);
							// 如果没有选中的话
							if (conditons == "") {
								alert("请勾选需要删除的配置");
								return;
							} else {
							
								if (confirm("确认删除名为["
										+ ids + "]的配置吗?")) {
									// 保存后台调用
									$.ajax({
												type : "POST",
												 url : basePath + "/data/deleteWarningConfig",
												dataType : "json",
												traditional: true,
												data :JSON.stringify(conditons),
												contentType : 'application/json;charset=utf-8',//指定为json类型 
												success : function(data) {
													alert("删除成功");
													loadData();
												}
											});

								}
							}
						}
						
	
						//保存配置
						this.saveConfig = function() {
							var basePath = $scope.getAppBasePath();
							var conditons = []; 
							var ids = "";
							$.each($(".check_item:checked"), function() {
								var contectsObj = new Object();   
								contectsObj.desc = $(this).parents("tr").find("td:eq(1)").text();
								contectsObj.warningLv1ValueLow = $(this).parents("tr").find("#lv1_low").val() || "";
								contectsObj.warningLv1ValueHigh = $(this).parents("tr").find("#lv1_high").val() || "";
								contectsObj.warningLv2ValueLow = $(this).parents("tr").find("#lv2_low").val() || "";
								contectsObj.warningLv2ValueHigh = $(this).parents("tr").find("#lv2_high").val() || "";
								contectsObj.warningLv3ValueLow = $(this).parents("tr").find("#lv3_low").val() || "";
								contectsObj.warningLv3ValueHigh = $(this).parents("tr").find("#lv3_high").val() || "";
								conditons.push(contectsObj);
								ids += $(this).parents("tr").find("td:eq(1)").text()+",";
							})
							ids = ids.substring(0,ids.length-1);
							// 如果没有选中的话
							if(conditons != "0"){
							if (conditons == "" ) {
								alert("请勾选需要修改的配置");
								return;
							} else {
							
								if (confirm("确认修改名为["
										+ ids + "]的配置吗?")) {
									// 保存后台调用
									$
											.ajax({
												type : "POST",
												url : basePath + "/data/updateWarningConfig",
												dataType : "json",
												contentType : "application/json",
												data : JSON.stringify(conditons),
												success : function(data) {
													alert("保存成功");
													loadData();
												}
											});

								}
							}
							}
						}
						
						// 搜索事件
						this.search = function() {
							loadData();
						}

						// 重置事件
						this.reset = function() {
							this.queryCondition = {
									"monitoringIndex" : "",
									"page" : 1,
									"size" : 5,
								};
						}
						//查看详情
						this.warinConfigDtail = function(data){
//							console.log(data);
							$scope.contentShow = data.cmd;
							$scope.kpi_name = data.name;
							//
							$('[data-toggle="popover"]').popover({ 
	                        	trigger:'click',//manual 触发方式
	            	            placement : 'left',  
	            	            title:'text',
	            	            html: 'true', 
	            	            content : "ulstring",  //这里可以直接写字符串，也可以 是一个函数，该函数返回一个字符串；
	            	            animation: false
	            	         }) .on("mouseenter", function () {
	            	                    var _this = this;
	            	                    $(this).popover("show");
	            	                    $(this).siblings(".popover").on("mouseleave", function () {
	            	                        $(_this).popover('hide');
	            	                    });
	            	          }).on("mouseleave", function () {
	            	                    var _this = this;
	            	                    setTimeout(function () {
	            	                        if (!$(".popover:hover").length) {
	            	                            $(_this).popover("hide")
	            	                        }
	            	                    }, 100);
	            	           });  
                              
							//弹框显示时触发事件
							 $('[data-toggle="popover"]').on('shown.bs.popover', function () {
									// console.log($("#sub"));
								    $("#sub").unbind();
									 $("#sub").click(function(){
//										
										 var a=$(".subContent").val();
//										    console.log(a);
										    $.ajax({
												type : "get",
												url : basePath +"/data/updateWarningKpiConfig",
												dataType : "json",
//												contentType : "application/json",
												data:{
													 "kpi_name": $scope.kpi_name,
													 "kpi_cmd":$(".subContent").val()
											    },
												success : function(data) {
													//手动刷新
													 $scope.$apply();  
													alert("更新成功！")
													loadData();
													console.log("000")
													},
												error:function(res){
													alert("请求错误！");
												}
												})
										  
									})
									$("#clean").click(function(){
										$(".subContent").val('');
									})
							})
							 
							 //
						}
						// 程序初期化
						this.init = function() {
							loadData();loadkpi();
							
						}
						this.init();

						
						//加载kpi
						function loadkpi(){
							var basePath = $scope.getAppBasePath();
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/getWarningMonitoringKpi",
							 dataType : "json",
							 contentType : "application/json",
							 //data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 controller.indexList = jsonResult;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });
						}
						
						// 加载数据
						function loadData() {
							if ($(".check_item:checked").length == 0) {
								$("#check_all").prop("checked", false);
							}
							
							var basePath = $scope.getAppBasePath();
							// 参数获取
							controller.parameter.monitoringIndex = controller.queryCondition.monitoringIndex ||"";
							controller.parameter.page = controller.queryCondition.page;
							controller.parameter.size = controller.queryCondition.size;
						
							// 后台调用
							 $.ajax({
							 type : "POST",
							 contentType : "application/json",
							 url : basePath + "/data/getWarningConfig",
							 dataType : "json",
							 data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 controller.warningConfigList = jsonResult.list;
							 controller.paginationInfo.totalPages = jsonResult.pages;
							 controller.paginationInfo.totalElements = jsonResult.total;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });

						}

					})
			.controller(
					'insertWarningConfigController',
					function($scope, $uibModalInstance, $http, $log) {
						var basePath = $scope.getAppBasePath();
						var controller = this;
						this.indexList = [];
						this.kpiList = [];
						
						// 程序初期化
						this.init = function() {
						}
						this.init();
						
						
					
				
						
						$scope.save = function() {
							var basePath = $scope.getAppBasePath();
							// INSERT参数定义
							insertComponent = {
								"desc" : "",
								"kpi" : "",
								"warningLv1ValueLow" : "",
								"warningLv1ValueHigh" : "",
								"warningLv2ValueLow" : "",
								"warningLv2ValueHigh" : "",
								"warningLv3ValueLow" : "",
								"warningLv3ValueHigh" : "",
								"kipType":"",
								"kpiCmd":""
							};
							// INSERT值获取
							insertComponent.desc = $scope.$parent.insertComponent.monitoringIndex;
							insertComponent.kpi = $scope.$parent.insertComponent.kpi;
							insertComponent.warningLv1ValueLow = $scope.$parent.insertComponent.lv1Low;
							insertComponent.warningLv1ValueHigh = $scope.$parent.insertComponent.lv1High;
							insertComponent.warningLv2ValueLow = $scope.$parent.insertComponent.lv2Low;
							insertComponent.warningLv2ValueHigh = $scope.$parent.insertComponent.lv2High;
							insertComponent.warningLv3ValueLow = $scope.$parent.insertComponent.lv3Low;
							insertComponent.warningLv3ValueHigh = $scope.$parent.insertComponent.lv3High;
							insertComponent.kpiType = $scope.$parent.insertComponent.type;
							insertComponent.kpiCmd = $scope.$parent.insertComponent.cmd;
							// 后台调用
							console.log(insertComponent);
							$.ajax({
								type : "POST",
								url : basePath + "/data/insertWarningConfig",
								dataType : "json",
								contentType : "application/json",
								data : JSON.stringify(insertComponent),
								success : function(data) {
									$uibModalInstance.close();
									alert("插入成功");
								}
							});
							
						};
						$scope.cancel = function() {
							// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
							$uibModalInstance.dismiss('cancel');
						};
					})
			
})();